import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/pages/Home/Home'  // @/ 代表绝对路径 src
import Login from '@/pages/Login/Login'  //
import Order from '@/pages/Order/Order'  //
import Page1 from '@/pages/Order/Page1/Page1'
import Page2 from '@/pages/Order/Page2/Page2'

Vue.use(Router)
// 配置路由
const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
      // 是否显示头部
      meta:{
        isHideHeader:true,
        isHideFooter:true
      }
    },
    // 一级路由
    {
      path: '/order',
      name: 'Order',
      component: Order,
      // 独立守卫
      beforeEnter:(to,from,next)=>{
        console.log("进入独享守卫");
        console.log(to);
        console.log(from);
        console.log(next);
        let isLogin = true;// 已登录
        if(isLogin == false){// 如果没有登录，不能进入订单页面
          next({path:"/login"});
        }
        next();
      },
      // 二级路由
      children:[
        {
          path:'page1/:id',
          component: Page1,
          // 独立守卫
          beforeEnter:(to,from,next)=>{
            console.log("进入独享守卫");
            if(to.params.id > 0){// 有id
              next();
            }else{
              next({path:"/login"});
            }
          },
        },
        {
          path:'page2',
          component: Page2
        }
      ]
    }
  ]
})
// 全局守卫
router.beforeEach((to,from,next)=>{

    if(to.path == "/login" || to.path == "/register"  || to.path == "/home"){// 不需要登录页面
      next();
    }else{// 需要登录页面
        if(localStorage.getItem("mall_token") ){// 已登录
            next();
        }else{
           // 角色 权限 的判断
           next({path:"/login"})
        }
    }

})
export default router
